package com.xxzx.journalism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxzx.journalism.entity.Infos;
import com.xxzx.journalism.mapper.InfosMapper;
import com.xxzx.journalism.model.vo.info.InfoListVO;
import com.xxzx.journalism.model.vo.info.InfoParamsVO;
import com.xxzx.journalism.model.vo.info.InfoVO;
import com.xxzx.journalism.service.InfosService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.DeleteBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
public class InfosServiceImpl extends ServiceImpl<InfosMapper, Infos> implements InfosService {

    @Autowired
    InfosMapper infosMapper;

    /**
     * 全量列表
     *
     * @return
     */
    @Override
    public List<InfoListVO> getAll(String type) {
        List<Infos> infos = infosMapper.selectList(new QueryWrapper<Infos>().eq("type", type).orderByDesc("utime"));
        List<InfoListVO> infoListVOList = new ArrayList<>();
        infos.forEach((info) -> {
            InfoListVO infoListVO = new InfoListVO();
            BeanUtils.copyProperties(info, infoListVO);
            infoListVOList.add(infoListVO);
        });
        return infoListVOList;
    }

    /**
     * 信息管理详情
     *
     * @param id
     * @return
     */
    @Override
    public InfoVO detail(String id) {
        Infos info = infosMapper.selectById(id);
        if (info == null) {
            throw new RuntimeException("找不到对应的信息管理信息");
        }
        InfoVO gatewayVO = new InfoVO();
        BeanUtils.copyProperties(info, gatewayVO);
        return gatewayVO;
    }

    /**
     * 保存信息管理数据
     *
     * @param infosParamsVO
     * @return
     */
    @Override
    public Integer save(InfoParamsVO infosParamsVO) {
        Integer id = infosParamsVO.getId();
        Infos info = new Infos();
        BeanUtils.copyProperties(infosParamsVO, info);
        if (id == null || id.equals("")) {
            infosMapper.insert(info);
            return info.getId();
        } else {
            infosMapper.update(info, new QueryWrapper<Infos>().eq("id", id));
            return infosParamsVO.getId();
        }
    }
}
